package com.art.sunflower.controller;


import com.art.sunflower.pojo.SchoolIntroduce;
import com.art.sunflower.service.IntroduceService;
import com.art.sunflower.util.AddFile;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping(value = "")
@Api(value = "学校首页")
public class IntroduceController {
    @Resource
    private IntroduceService introduceService;


    @GetMapping("introduce")
    @ApiOperation("获取首页信息")
    public Message selectById(@RequestParam("id") int id) {
        if (introduceService.selectById(id) == null) {
            return Message.createErr("编号不存在，查找失败");
        } else {
            return Message.createSuc(introduceService.selectById(id));
        }
    }

    @PutMapping("introduce")
    @RequiresRoles(value = {"admin"})
    @ApiOperation("修改首页信息")
    public Message updateIntroduceById(@RequestParam(value = "file", required = false) MultipartFile file, int id, String profile, String history, String concept, String introduce) {
        SchoolIntroduce schoolIntroduce = introduceService.selectById(id);
        if (schoolIntroduce != null) {
            schoolIntroduce.setProfile(profile);
            schoolIntroduce.setHistory(history);
            schoolIntroduce.setConcept(concept);
            schoolIntroduce.setIntroduce(introduce);
            if (file != null) {
                AddFile addFile = new AddFile();
                schoolIntroduce.setUrl(addFile.addFile(file, "school_introduce"));
            }
        }
        int count = introduceService.updateIntroduceById(schoolIntroduce);
        if (count > 0) {
            return Message.createSuc("修改成功");
        }
        return Message.createErr("修改失败");
    }


}

